<?php
namespace app\admin\controller;
use think\exception\HttpResponseException;
use think\facade\Request;
use app\service\commonService;
use auth\Auth;
use app\BaseController;
class Common extends BaseController{
    public function initialize(){
        //获取当前操作控制器名称和方法名称
        $request = Request::instance();
        $con = $request->controller();
        $action = $request->action();
        $name = $con .'/'. $action;
//        $url=url('admin/login/login');
//        var_dump(session());
        //var_dump(session('id'));exit;
		if(!session('id') || !session('name')){
            throw  new HttpResponseException(redirect(url('admin/login/login')));
            return redirect(url('admin/login/login'));
           // $this->error($msg = '登录失败',  $url =  $url);
            //$this->success('您尚未登录系统',url('admin/login/login'));
            return $this->error('您尚未登录系统',url('admin/login/login'));
            //$this->redirect(url('admin/login/login'));
            return $this->error('您尚未登录系统',url('admin/login/login'));
        } 

        if($name !== 'Room/fetchdata' && $name !== 'Index/superedit' && $name !== 'Index/filedel' && $name !== 'Room/fetchdata'){
            if(!session('id') || !session('name')){
                throw  new HttpResponseException(redirect(url('admin/login/login')));
                $this->error('您尚未登录系统',url('admin/login/login'));
            } 
        }
        $auth = (new Auth())->check($name,session('id'));
        //可以直接访问的权限
        $notCheck = array('Apiuser/apiuser','Index/superedit','Index/filedel','Room/fetchdata','Door/door','Index/index','Index/welcome');
        //超级管理员可以访问所有权限
        //dump(session('id'));die;
        if(session('id')!==1){
            if(!in_array($name,$notCheck)){
                if(!$auth){
                    throw  new HttpResponseException(redirect(url('index/index')));
                    $this->error('没有权限访问',url('index/index'));
                }
            }
        }
    }
    //根据当前账号信息查询token
    public function pidTokenId(){
        $id = session('id');
        //根据ID查询用户信息
        $adminId = (new commonService())->adminIdInfo($id);
        $usertype = $adminId['usertype'];//角色类型
        $department = $adminId['department'];//所属部门
        if($usertype == 0){//超级管理员
            //查询所有服务商
            $com['service_id'] = (new commonService())->serviceList();
            //查询所有物业
            $com['property_id'] = (new commonService())->propertyList();
            //查询所有小区
            $com['community_id'] = (new commonService())->communityList();
        }else if($usertype == 1){//服务商
            $com['service_id'] = $department;
            //查询所有物业
            $com['property_id'] = (new commonService())->serviceIdProperty($department);
            //查询所有小区
            $com['community_id'] = (new commonService())->propertyIdCommunity($com['property_id']);
        }else if($usertype == 2){//物业
            //查询所有小区
            $com['property_id'] = $department;
            $com['community_id'] = (new commonService())->propertyIdCommunity($department);
        }else if($usertype == 3){//小区
            $com['community_id'] = $department;
        }
        return $com;
    }
}